package com.gopay.scheduler.job;

import com.gopay.common.constants.cbrmb.CbRmb01736Step;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.cbrmb.Tran01736VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import com.gopay.scheduler.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 跨境人民币进口分账订单异步账务处理Job
 * @Class CbRmbImportLedgerProcessJob.java
 * @Author b.zou
 * @Date 2017-7-25 www.gopay.com.cn Inc.All rights reserved.
 */
public class CbRmbImportLedgerProcessJob extends GopayBaseJob {

    // 日志
    private Logger logger = LoggerFactory.getLogger(CbRmbImportLedgerProcessJob.class);

    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

    @Resource(name = "cpsGeneralMainOrderQueryDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderQueryDAO;

    @Override
    protected void doJobInternal() {
        logger.info("======= CbRmbImportLedgerProcessJob(跨境人民币进口分账定时任务)  BEGIN  ===========");
        //查询需要定时任务做账的订单
        List<CpsGenMainOrder> list = this.getAccountProcessMainOrder();
        if(list!=null&&list.size()>0){
            logger.error("******* CbRmbImportLedgerProcessJob(跨境人民币进口分账定时任务)--需要进行账务处理订单数量：{} *******",list.size());
            // 调用CPS
            Object paramVo;
            CPSResponse response;
            for (CpsGenMainOrder main : list) {
                // 封装cps请求参数vo
                paramVo = buildCallCpsParamVo(main);
                logger.error("******* CbRmbImportLedgerProcessJob(跨境人民币进口分账定时任务)--需要进行账务订单订单号：{} *******",main.getGopayOrderId());
                try {
                    // 调用cps
                    response = callCPSHelper.callCbRmbByVOTicket(paramVo);
                    if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                        logger.error("------调用cps返回异常  response=" + response.getRespCode());
                    }
                } catch (Exception e) {
                    // 吃掉所有异常
                    logger.error("callByVOTicket Error", e);
                }

            }
        }else{
            logger.info("******* CbRmbImportLedgerProcessJob(跨境人民币进口分账定时任务)--没有跨境人民币进口分账订单需要进行账务及后续处理 *******");
        }
        logger.info("======= CbRmbImportLedgerProcessJob(跨境人民币进口分账定时任务)   END   ===========");
    }

    private List<CpsGenMainOrder> getAccountProcessMainOrder(){
        Calendar timeBegin = Calendar.getInstance();
        Calendar timeEnd = Calendar.getInstance();
        timeBegin.add(Calendar.DATE,-2);
        String timeBeginString = DateUtils.formatDate(timeBegin.getTime(),"yyyyMMddHHmmss");
        String timeEndString = DateUtils.formatDate(timeEnd.getTime(),"yyyyMMddHHmmss");
        Map<String,Object> params = new HashMap<>();
        params.put("intTxnCd", IntTxnCd._01736.value);
        params.put("txnStaCd", TxnStaCode.TXN_STA_CD_30000.value);
        params.put("gopayTxnTmBegin", timeBeginString);
        params.put("gopayTxnTmEnd", timeEndString);
        String hql = " from CpsGenMainOrder t where t.gopayIntTxnCd=:intTxnCd and t.txnStaCd=:txnStaCd and t.gopayTxnTm>=to_timestamp(:gopayTxnTmBegin, 'yyyymmddHH24miss') and" +
                " t.gopayTxnTm<=to_timestamp(:gopayTxnTmEnd, 'yyyymmddHH24miss')";
        List<CpsGenMainOrder> mainOrderList = cpsGeneralMainOrderQueryDAO.findByHql(hql,params);
        return mainOrderList;
    }

    /**
     * 封装cps请求参数vo
     * @param main
     * @return
     */
    private Object buildCallCpsParamVo(CpsGenMainOrder main) {
        String gopayOrderId = main.getGopayOrderId();
        Object obj = null;
        Tran01736VO paramVo = new Tran01736VO();
        paramVo.setGopayOrderId(gopayOrderId);
        paramVo.setCbRmb01736Step(CbRmb01736Step.ASYNCHRONOUS_ACCOUNT_PROCESSING);
        obj = paramVo;
        return obj;
    }
}
